﻿//----------------------------------------------------------------------------------------------------
// Visual Node Editor is used in the Editor state for editing of node specific options
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.events.*;
	
	import HG.GameService;
	import HG.Managers.GameRuleManager;
	
	public class VisualLevelConfigurator extends VisualObject
	{
		var m_gameRuleManager:GameRuleManager;
		
		var m_level:VisualLevel;
		
		var m_selectedGameType:Number;
		
		//----------------------------------------------------------------------------------------------------
		public function VisualLevelConfigurator()
		{
			m_gameRuleManager = GameService.GetInstance().GetGameRuleManager();
			
			Fader.addEventListener(MouseEvent.MOUSE_UP, OnFaderClick);
			Button_Apply.addEventListener(MouseEvent.MOUSE_UP, OnButton_ApplyClick);
			
			// encryption
			GameType_Next.addEventListener(MouseEvent.MOUSE_UP, OnGameType_NextClick);
			GameType_Prev.addEventListener(MouseEvent.MOUSE_UP, OnGameType_PrevClick);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Open(visLevel:VisualLevel)
		{
			m_level = visLevel;
			SetVisible(true);
			
			m_selectedGameType = m_level.GetGameType();
			TXT_Name.text = m_level.GetName();
			
			var types:Array = m_gameRuleManager.GetGameTypes();
			TXT_GameType.text = types[m_selectedGameType].GetName();
			TXT_GameTypeDesc.text = types[m_selectedGameType].GetDescription();
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnFaderClick(evt:MouseEvent)
		{
			SetVisible(false);
		}
		
		//----------------------------------------------------------------------------------------------------
		// GameType
		//----------------------------------------------------------------------------------------------------
		private function OnGameType_NextClick(evt:MouseEvent)
		{
			var types:Array = m_gameRuleManager.GetGameTypes();
			
			m_selectedGameType++;
			
			if (m_selectedGameType >= types.length)
				m_selectedGameType = 0;
				
			TXT_GameType.text = types[m_selectedGameType].GetName();
			TXT_GameTypeDesc.text = types[m_selectedGameType].GetDescription();
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnGameType_PrevClick(evt:MouseEvent)
		{
			var types:Array = m_gameRuleManager.GetGameTypes();
			
			m_selectedGameType--;
			
			if (m_selectedGameType < 0)
				m_selectedGameType = types.length;
				
			TXT_GameType.text = types[m_selectedGameType].GetName();
			TXT_GameTypeDesc.text = types[m_selectedGameType].GetDescription();
		}
		
		//----------------------------------------------------------------------------------------------------
		// Actions
		//----------------------------------------------------------------------------------------------------
		private function OnButton_ApplyClick(evt:MouseEvent)
		{
			m_level.SetName(TXT_Name.text);
			m_level.SetGameType(m_selectedGameType);
			
			
			SetVisible(false);
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}